Coverage Report

Created: 2021-08-28 18:14

D:\git\skunkworks\herald-for-cpp\herald\src\datatype\uuid.cpp
Line
Count
Source (jump to first uncovered line)
1
//  Copyright 2020-2021 Herald Project Contributors
2
//  SPDX-License-Identifier: Apache-2.0
3
//
4
5
#include "herald/datatype/uuid.h"
6
#include "herald/datatype/data.h"
7
#include "herald/datatype/randomness.h"
8
#include "herald/datatype/base64_string.h"
9
10
#include <string>
11
#include <algorithm>
12
#include <array>
13
#include <sstream>
14
#include <iosfwd>
15
#include <iomanip>
16
17
#include <type_traits>
18
19
namespace herald {
20
namespace datatype {
21
22
// PIMPL Class
23
// class UUID::Impl {
24
// public:
25
//   using value_type = uint8_t;
26
27
//   Impl(std::array<value_type, 16>& data, bool isValid);
28
//   ~Impl();
29
30
//   std::array<value_type, 16> mData = { {0}};
31
//   bool mValid;
32
// };
33
34
// UUID::Impl::Impl(std::array<value_type, 16>& data, bool isValid)
35
//  : mValid(isValid)
36
// {
37
//   mData = std::move(data);
38
// }
39
40
// UUID::Impl::~Impl() {
41
//   ;
42
// }
43
44
45
46
// Implementation Class
47
48
// Static functions
49
UUID
50
136
UUID::fromString(const std::string& from) noexcept {
51
136
  Base64String asString;
52
136
  // remove hyphens before using hex decoding
53
136
  std::string newFrom = from; // copy
54
136
  newFrom.erase(std::remove(newFrom.begin(),newFrom.end(),'-'), newFrom.end());
55
136
  auto dataInstance = Data::fromHexEncodedString(newFrom);
56
136
57
136
  std::array<value_type, 16> data{ {0} };
58
136
  if (dataInstance.size() != 16) {
59
2
    return UUID(data,false);
60
2
  }
61
2.27k
  
for (std::size_t pos = 0;134
pos < 16;
++pos2.14k
) {
62
2.14k
    data[pos] = (value_type)dataInstance.at(pos);
63
2.14k
  }
64
134
  UUID uuid(data,true); // TODO check UUID is a V4 format
65
134
  return uuid;
66
134
}
67
68
// Instance functions
69
UUID::UUID(const char* from) noexcept
70
 : mData({0}), mValid(true)
71
70
{
72
70
  // TODO actually copy the value from the string
73
70
}
74
75
UUID::UUID(UUID&& from) noexcept
76
 : mData(std::move(from.mData)), mValid(from.mValid)
77
134
{
78
134
  ;
79
134
}
80
81
UUID::UUID(const UUID& from) noexcept
82
 : mData(from.mData),mValid(from.mValid)
83
20
{
84
20
  ;
85
20
}
86
87
// private ctor
88
UUID::UUID(std::array<value_type, 16> data, bool isValid) noexcept
89
 : mData(data),mValid(isValid)
90
141
{
91
141
  ;
92
141
}
93
94
95
UUID&
96
UUID::operator=(const UUID& other) noexcept
97
0
{
98
0
  mData = other.mData;
99
0
  mValid = other.mValid;
100
0
  return *this;
101
0
}
102
103
bool
104
0
UUID::valid() const noexcept {
105
0
  return mValid;
106
0
}
107
108
bool
109
47
UUID::operator==(const UUID& other) const noexcept {
110
47
  return mData == other.mData;
111
47
}
112
bool
113
2
UUID::operator!=(const UUID& other) const noexcept {
114
2
  return mData != other.mData;
115
2
}
116
117
bool
118
0
UUID::operator<(const UUID& other) const noexcept {
119
0
  return mData < other.mData;
120
0
}
121
122
bool
123
0
UUID::operator>(const UUID& other) const noexcept {
124
0
  return mData > other.mData;
125
0
}
126
127
std::array<uint8_t, 16>
128
0
UUID::data() const noexcept {
129
0
  return mData;
130
0
}
131
132
std::string
133
2
UUID::string() const noexcept {
134
2
  // convert bytes to hex string
135
2
  std::stringstream str;
136
2
  str.setf(std::ios_base::hex, std::ios::basefield);
137
2
  str.fill('0');
138
34
  for (std::size_t i=0; i < 16; 
i++32
) {
139
32
    str << std::setw(2) << (unsigned short)mData[i];
140
32
  }
141
2
  std::string hexString = str.str();
142
2
  // add in hyphens at relevant points
143
2
  std::stringstream fstr;
144
2
  fstr << hexString.substr(0,8) << "-" << hexString.substr(8,4) << "-"
145
2
       << hexString.substr(12,4) << "-" << hexString.substr(16,4) << "-"
146
2
       << hexString.substr(20,12);
147
2
  return fstr.str();
148
2
}
149
150
// Static assertions on this classes compiler contract
151
static_assert(std::is_constructible_v<UUID,const char*>,"UUID Cannot be string constructed");
152
static_assert(std::is_copy_constructible_v<UUID>,"UUID Cannot be copy constructed");
153
static_assert(std::is_move_constructible_v<UUID>,"UUID Cannot be move constructed");
154
155
} // end namespace
156
} // end namespace